﻿//-----------------------------------------------------------------------
// <copyright file="AppDomainCustomizer!1.cs">
//     Copyright (c) Nicole Calinoiu. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Diagnostics.CodeAnalysis;

namespace Bordecal.FxCop.Sdk.Testing.AppDomainCustomization
{
	[Serializable]
	[SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented",
			Justification = "False positive.")]
	internal abstract class AppDomainCustomizer<TData>
	{
		protected AppDomainCustomizer()
		{
		}

		private string DataKey
		{
			get
			{
				return this.GetType().FullName;
			}
		}

		internal void CustomizeAppDomain(AppDomain appDomain)
		{
			if (appDomain == null)
			{
				throw new ArgumentNullException("appDomain");
			}

			appDomain.SetData(this.DataKey, this.CreateDataValue());
			appDomain.DoCallBack(new CrossAppDomainDelegate(this.CustomizeCurrentAppDomain));
		}

		protected abstract TData CreateDataValue();

		private void CustomizeCurrentAppDomain()
		{
			AppDomain currentDomain = AppDomain.CurrentDomain;
			this.DoCustomizeAppDomain(currentDomain, (TData)currentDomain.GetData(this.DataKey));
		}

		protected abstract void DoCustomizeAppDomain(AppDomain targetDomain, TData data);
	}
}